<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>公共前缀</title>
</head>

<body>
  <script>
    function num(arr) {
      let res = "", temp = "";
      for (let i = 0; i < arr[0].length; i++) {  // 以数组第一个元素为标准
        res += arr[0][i];
        for (let j = 1; j < arr.length; j++) {  // 如果后面所有元素都以该前缀开头，则前缀++
          if (arr[j].indexOf(res) !== 0) {
            return temp  // 否则返回 temp
          }
        }
        temp = res;
      }
      return res;
    }
    console.log(num(["flower", "flow", "flight"]));
  </script>
</body>

</html>